using System;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmAddOrder : Form {
        private OrderDeskBLL _orderDeskBLL = new OrderDeskBLL();
        private OrderBLL _orderBll = OrderBLL.Instance;
        private DeskBLL _deskBll = DeskBLL.Instance;
        private RoomAndDesk _roomAndDesk;
        public FrmAddOrder() {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        public void InitFrmAddOrder(RoomAndDesk roomAndDesk) {
            _roomAndDesk = roomAndDesk;
            label6.Text = roomAndDesk.Room.RoomName;
            label7.Text = roomAndDesk.Desk.DeskName;
            label10.Text = roomAndDesk.Room.RoomMinConsumption.ToString();
        }

        private void btncancel_Click(object sender, EventArgs e) {
            this.Close();
        }

        private event Action<RADwithOrderId> TransToFrmOrderWithProduct;
        private void btnOK_Click(object sender, EventArgs e) {
            //新增订单
            int orderId = _orderBll.AddOrder(new Order() {
                NumberOfDiners = int.Parse(txtPersonCount.Text),
                Remark = txtDescription.Text,
                SubTime = DateTime.Now
            });
            //将桌子设置为非空闲
            _roomAndDesk.Desk.DeskState = 1;
            _deskBll.UpdateDeskState(_roomAndDesk.Desk);
            //为订单_桌子表添加数据
            _orderDeskBLL.Add(new OrderDesk() {
                OrderId = orderId,
                DeskId = _roomAndDesk.Desk.DeskId
            });
            //是否为订单添加消费
            if (ckbMeal.Checked) {
                FrmOrderWithProduct _frmOrderWithProduct = new FrmOrderWithProduct();
                _frmOrderWithProduct.Closing += (o, s) => { this.Close(); };
                TransToFrmOrderWithProduct += _frmOrderWithProduct.InitFrm;
                TransToFrmOrderWithProduct.Invoke(new RADwithOrderId() {
                    OrderId = orderId,
                    Desk = _roomAndDesk.Desk,
                    Room = _roomAndDesk.Room
                });
                this.Hide();
                _frmOrderWithProduct.ShowDialog();
            }
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void FrmAddOrder_Load(object sender, EventArgs e) {
            
        }
    }
}